
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.FreightDao;
import com.sun.showcase.biz.mall.service.FreightService;
import com.sun.showcase.client.domain.mall.Freight;
import com.sun.showcase.client.query.mall.FreightQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("freightService")
@Transactional
public class FreightServiceImpl implements FreightService{
	@Autowired
	private FreightDao freightDao;
	
	public void setFreightDao(FreightDao dao) {
		this.freightDao = dao;
	}

	public DataGrid datagrid(FreightQuery freightQuery) {
		DataGrid j = new DataGrid();
		Pager<Freight> pager  = find(freightQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<FreightQuery> getQuerysFromEntitys(List<Freight> freights) {
		List<FreightQuery> freightQuerys = new ArrayList<FreightQuery>();
		if (freights != null && freights.size() > 0) {
			for (Freight tb : freights) {
				FreightQuery b = new FreightQuery();
				BeanUtils.copyProperties(tb, b);
				freightQuerys.add(b);
			}
		}
		return freightQuerys;
	}

	private Pager<Freight> find(FreightQuery freightQuery) {
		Page<Freight> page = PageHelper.startPage(freightQuery.getPage().intValue(), freightQuery.getRows().intValue());
		List<Freight> list = freightDao.findList(freightQuery);
		Pager<Freight> pager = new Pager<Freight>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Freight add(FreightQuery freightQuery) {
		Freight t = new Freight();
		BeanUtils.copyProperties(freightQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		freightDao.save(t);
		freightQuery.setId(t.getId());
		return t;
		
	}

	public void update(FreightQuery freightQuery) {
		Freight t = freightDao.getById(freightQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(freightQuery, t);
	    	freightDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(FreightQuery freightQuery) {
	    freightDao.updatePart(freightQuery);
	}

	public void delete(String[] ids) {
		freightDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		freightDao.deletePt(ids);
	}

	public Freight get(FreightQuery freightQuery) {
		return freightDao.getById(freightQuery.getId());
	}

	public Freight get(String id) {
		return freightDao.getById(id);
	}

	
	public List<FreightQuery> listAll(FreightQuery freightQuery) {
	    List<Freight> list = freightDao.findList(freightQuery);
		List<FreightQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
